<template>
  <div class="login" :style="background">
    <el-container>
      <el-main>
        <router-link to="/blogs">
          <img src="../assets/img/Logo.png"
               style="height: 60%; margin-top: 10px; position: relative; left: 37.5%; top: -10%"/>
        </router-link>
        <el-form :model= "ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" size="mini"
                 class="demo-ruleForm">
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" type="text" maxlength="12" v-model="ruleForm.username" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-unlock" type="password" v-model="ruleForm.password" autocomplete="off" placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: '',
        username: ''
      },
      rules: {
        password: [
          {validator: validatePass, trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'}
        ]
      },
      background: {
        backgroundImage: 'url(' + require('../assets/img/Login.png') + ')',
        backgroundRepeat: "no-repeat",
        weight: "100%",
        height: "100%",
      },
    };
  },
  methods: {
    submitForm(formName) {
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 提交逻辑
          this.$axios.post(_this.GLOBAL.serverPath + '/login', this.ruleForm).then((res)=>{
            const token = res.headers['authorization']
            _this.$store.commit('SET_TOKEN', token)
            _this.$store.commit('SET_USERINFO', res.data.data)
            _this.$router.push("/blogs")
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
}
</script>
<style>
  .login{
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
  }
  Form{
    position: absolute;
    top: 10%;
    left: 36.66%;
    margin: 0 auto;
    width:350px;
    height: 250px;
    border: 0px solid grey;
    padding:50px 0;
    border-radius: 10px;
    text-align: center;
    background: #00000060;
  }
  .el-input {
    position: relative;
    top: 100px;
    font-size: 14px;
    display: inline-block;
    width: 100%;
    right: 45px;
  }
  .el-button{
    position: relative;
    top: 120px;
    right: 45px;
  }
  .el-form-item__error {
    color: #F56C6C;
    font-size: 12px;
    line-height: 1;
    padding-top: 4px;
    position: absolute;
    top: 130px;
    left: -40px;
  }
</style>

